package com.bdmc.web.controller.alarm;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.bdmc.alarm.domain.bo.AlarmEventBo;
import com.bdmc.alarm.domain.vo.AlarmEventVo;
import com.bdmc.alarm.service.IAlarmDetailService;
import com.bdmc.alarm.service.IAlarmEventService;
import com.bdmc.base.domain.BaseDevice;
import com.bdmc.base.service.IBaseDeviceService;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.device.domain.bo.DevGpsHistoryBo;
import com.bdmc.device.domain.vo.DevGpsHistoryVo;
import com.bdmc.device.service.IDevGpsHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 告警事件控制器
 *
 * @author bdmc
 * @date 2022-03-10
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/alarm/event")
public class AlarmEventController extends BaseController {

    private final IAlarmEventService iAlarmEventService;

    private final IAlarmDetailService detailService;

    private final IBaseDeviceService deviceService;

    @Autowired
    IDevGpsHistoryService gpsHistoryService;

    /**
     * 查询告警事件列表
     */
    @SaCheckPermission("alarm:event:list")
    @GetMapping("/list")
    public TableDataInfo<AlarmEventVo> list(@Validated(QueryGroup.class) AlarmEventBo bo, PageQuery pageQuery) {
        return iAlarmEventService.mySelectPage(bo, pageQuery);
    }



    /**
     * 导出告警事件列表
     */
    @SaCheckPermission("alarm:event:export")
    @Log(title = "告警事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated AlarmEventBo bo, HttpServletResponse response) {
        List<AlarmEventVo> list = iAlarmEventService.queryList(bo);
        ExcelUtil.exportExcel(list, "告警事件", AlarmEventVo.class, response);
    }

    /**
     * 获取告警事件详细信息
     */
    @SaCheckPermission("alarm:event:query")
    @GetMapping("/{eventId}")
    public AjaxResult<AlarmEventVo> getInfo(@PathVariable("eventId") Long eventId) {
        AlarmEventVo eventVo = iAlarmEventService.queryById(eventId);
        final BaseDevice device = deviceService.getById(eventVo.getDeviceId());
        DevGpsHistoryBo gpsHistoryBo = new DevGpsHistoryBo();
        gpsHistoryBo.setDevicePhoneNum(device.getDevicePhoneNum());
        gpsHistoryBo.setBeginTime(LocalDateTimeUtil.formatNormal(eventVo.getEventStart()));
        gpsHistoryBo.setEndTime(LocalDateTimeUtil.formatNormal(eventVo.getEventEnd()));
        final List<DevGpsHistoryVo> gpsHistoryVos = gpsHistoryService.traceList(gpsHistoryBo);
        eventVo.setAlarmDetailList(gpsHistoryVos);
        return AjaxResult.success(eventVo);
    }

    /**
     * 新增告警事件
     */
    @SaCheckPermission("alarm:event:add")
    @Log(title = "告警事件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody AlarmEventBo bo) {
        return toAjax(iAlarmEventService.insertByBo(bo));
    }

    /**
     * 修改告警事件
     */
    @SaCheckPermission("alarm:event:edit")
    @Log(title = "告警事件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody AlarmEventBo bo) {
        return toAjax(iAlarmEventService.updateByBo(bo));
    }

    /**
     * 删除告警事件
     */
    @SaCheckPermission("alarm:event:remove")
    @Log(title = "告警事件" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{eventIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] eventIds) {
        return toAjax(iAlarmEventService.deleteWithValidByIds(Arrays.asList(eventIds), true));
    }
}
